﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CARE.Web.Services;
using CARE.Views.Wizard.Setup;
using CARE.GraphProviderServiceReference;
using System.Collections.Generic;
namespace CARE
{
	public partial class SetupSimulation : UserControl
	{
        private GraphProviderServiceClient gp;
        private SpreadVizualization spreadVizualization;
        private StateChangePlanWindow scpw;
        public SetupSimulation(SpreadVizualization sw)
		{
			// Required to initialize variables
			InitializeComponent();
            gp = new GraphProviderServiceClient();
            spreadVizualization = sw;
            scpw = new StateChangePlanWindow();
            gp.GetAvailableStateChangeStrategiesCompleted += new EventHandler<GetAvailableStateChangeStrategiesCompletedEventArgs>(gp_GetAvailableStateChangeStrategiesCompleted);
            gp.GetAvailableStateChangeStrategiesAsync();
		}

        void gp_GetAvailableStateChangeStrategiesCompleted(object sender, GetAvailableStateChangeStrategiesCompletedEventArgs e)
        {
            List<String> invokeOpGetAvaliableStateChangeMethods = e.Result as List<String>;
            inculationChooser.Items.Clear();
            foreach (var item in invokeOpGetAvaliableStateChangeMethods)
                inculationChooser.Items.Add(item);
        }

		private void Steps_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
		{
            SimulationParameters sp = SimulationParameters.getInstance();
            sp.SimSteps = int.Parse(Steps.Text);
            var stopcon = new StopConditionSL()
            {
                stopConditionName = "EpidemicSimulator.stopconditions.MaxSimStepNumberStopCondition",
                initParams = new List<object>()
            };
            stopcon.initParams.Add(sp.SimSteps);
            sp.stopConditionsSL.Add(stopcon);
		}

		private void MinContacts_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
		{
            SimulationParameters sp = SimulationParameters.getInstance();
            sp.MinContacts = int.Parse(MinContacts.Text);
		}

		private void MaxContacts_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
		{
            SimulationParameters sp = SimulationParameters.getInstance();
            sp.MaxContacts = int.Parse(MaxContacts.Text);
		}

		private void vaccines_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
		{
			// TODO: Add event handler implementation here.
		}

		private void inculationChooser_SelectionChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
		{
           // btnChangePlan.Visibility = Visibility.Visible;
            btnChangePlan.IsEnabled = true;
		}

		private void btnChangePlan_Click(object sender, System.Windows.RoutedEventArgs e)
		{
            scpw.Closed += new EventHandler(scpw_Closed);
            scpw.Show();
		}

        void scpw_Closed(object sender, EventArgs e)
        {
            StateChangePlanSL plan = new StateChangePlanSL();
            plan.stateChangeStrategyName = "EpidemicSimulator.strategies." + inculationChooser.SelectedItem.ToString();
            plan.inPersents = scpw.Percentage.IsChecked.Value;
            plan.value = Convert.ToInt32(scpw.Val.Text);
            SimulationParameters.getInstance().stateChangePlansSL.Add(plan);
            strategies.Children.Add(new TextBlock()
            {
                Text = (strategies.Children.Count + 1).ToString() + ". "
                    + inculationChooser.SelectedItem.ToString() + ": "
                    + plan.value + " %",
                Margin = new Thickness(0, 3, 0, 0)
            });
        }
	}
}